﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using MiniJSON;

public class AdResult
{
	public int code;
	public string err;
}

public static class AdResultCode
{
	public const int Suc = 0;
	public const int Fail = 1;
	public const int Close = 2;
}

public interface IAdSdKCallback
{	
	Action onInitSuc { get; set; }
	Action<string> onInitFail { get; set; }
	
	Action onBannerShowSuc { get; set; }
	Action<string> onBannerShowFail { get; set; }
	Action onFullScreenShowSuc { get; set; }
	Action<string> onFullScreenShowFail { get; set; }
	Action onFullScreenClose { get; set; }
	
	Action onVideoLoadSuc { get; set; }
	Action<string> onVideoLoadFail { get; set; }
	Action onVideoSuc { get; set; }
	
	Action<string> onVideoFail { get; set; }
	Action onVideoClose { get; set; }
}

public class AdParseResult
{
	public int code;
	public string err;
}
public class AdSDKCallback : MonoBehaviour,IAdSdKCallback {
	
	public Action onInitSuc { get; set; }
	public Action<string> onInitFail { get; set; }
	public Action onBannerShowSuc { get; set; }
	public Action<string> onBannerShowFail { get; set; }
	public Action onFullScreenLoadSuc { get; set; }
	public Action<string> onFullScreenLoadFail { get; set; }
	public Action onFullScreenShowSuc { get; set; }
	public Action<string> onFullScreenShowFail { get; set; }
	public Action onFullScreenClose { get; set; }
	public Action onVideoLoadSuc { get; set; }
	public Action<string> onVideoLoadFail { get; set; }
	public Action onVideoSuc { get; set; }
	public Action<string> onVideoFail { get; set; }
	public Action onVideoClose { get; set; }

	private void Awake()
	{
		DontDestroyOnLoad(gameObject);
	}

	// Use this for initialization
	void Start () {
		
	}

	public void TestCallback(string content)
	{
		Debug.Log($"TestCallback:{content}");

	}

	public void OnInitAdResult(string result)
	{
		Debug.Log($"OnInitAdResult : {result}");
		
		ParseAdResult(result, onInitSuc, onInitFail, null,"InitAd");

		onInitSuc = null;
		onInitFail = null;
	}

	public void OnBannnerAdShowResult(string dict)
	{
		Debug.Log($"OnBannnerAdShowResult:{dict}");
		
		ParseAdResult(dict, this.onBannerShowSuc, onBannerShowFail, null,"ShowBanner");
	}
	
	public void OnFullScreenAdLoadResult(string dict)
	{
		Debug.Log($"OnFullScreenAdLoadResult:{dict}");
		
		ParseAdResult(dict, this.onFullScreenLoadSuc, onFullScreenLoadFail, null,"LoadFullScreen");
	}
	public void OnFullScreenAdShowResult(string dict)
	{
		Debug.Log($"OnFullScreenAdShowResult:{dict}");
		
		ParseAdResult(dict, this.onFullScreenShowSuc, onFullScreenShowFail, onFullScreenClose,"FullScreen");
	}
	
	public void OnRewardAdLoadResult(string dict)
	{
		Debug.Log($"OnRewardAdLoadResult:{dict}");

		var onSuc = this.onVideoLoadSuc;
		var onFail = this.onVideoLoadFail;
		
		//this.onVideoLoadSuc = null;
		//this.onVideoLoadFail = null;
		
		ParseAdResult(dict, onSuc, onFail, null,"LoadRewardVideo");
	}
	public void OnRewardAdPlayResult(string dict)
	{
		Debug.Log($"OnRewardAdPlayResult:{dict}");

		var onSuc = this.onVideoSuc;
		var onFail = this.onVideoFail;
		var onClose = this.onVideoClose;
		
		ParseAdResult(dict, onSuc, onFail, onClose,"RewardVideo");
		
		/*this.onVideoSuc = null;
		this.onVideoFail = null;
		this.onVideoClose = null;*/
	}
	
	
	void ParseAdResult(string dict,Action onSuc,Action<string>onFail,Action onClose,string logName)
	{
		AdParseResult adResult = null;
		try
		{
			adResult = Newtonsoft.Json.JsonConvert.DeserializeObject<AdParseResult>(dict); // MiniJSON.Json.Deserialize(dict) as Dictionary<string,object>;
		}
		catch (Exception e)
		{
			Debug.LogError($"parse ad result error");
			onFail?.Invoke("arse ad result error");
		}

		int code = adResult.code;
		string error = adResult.err;
		
		try
		{
			if (code == AdResultCode.Suc)
			{
				Debug.Log($"{logName} ad suc!");
				onSuc?.Invoke();
			}
			else if (code == AdResultCode.Fail)
			{
				Debug.Log($"{logName} ad fail");
				onFail?.Invoke(error);
			}
			else if (code == AdResultCode.Close)
			{
				Debug.Log($"{logName} ad close");
				onClose?.Invoke();
			}
			else
			{
				Debug.LogError($"invalid code:{code}  err:{error}");
				onFail?.Invoke(error);
			}
		}
		catch (Exception e)
		{
			Debug.LogError(e);
		}
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

